package com.jdrx.sw.sewage.service;

import com.github.pagehelper.PageHelper;
import com.jdrx.platform.commons.rest.beans.dto.IdDTO;
import com.jdrx.sw.sewage.beans.dto.WaterQualityTestAddDTO;
import com.jdrx.sw.sewage.beans.dto.WaterQualityTestQueryDTO;
import com.jdrx.sw.sewage.beans.dto.WaterQualityTestUpdateDTO;
import com.jdrx.sw.sewage.beans.entity.WaterQualityTestPO;
import com.jdrx.sw.sewage.beans.vo.base.PageVO;
import com.jdrx.sw.sewage.dao.WaterQualityTestDAO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class WaterQualityTestService {
    @Autowired
    private WaterQualityTestDAO waterQualityTestDAO;

    public void add(WaterQualityTestAddDTO dto) {
        WaterQualityTestPO po = new WaterQualityTestPO();
        BeanUtils.copyProperties(dto, po);
        // 报表名称=前端传入的模板名称+年月日时分秒
        po.setName(dto.getName() + DateTimeFormatter.ofPattern("yyyyMMddHHmmss").format(LocalDateTime.now()));
        waterQualityTestDAO.insert(po);
    }

    public void update(WaterQualityTestUpdateDTO dto) {
        WaterQualityTestPO po = new WaterQualityTestPO();
        BeanUtils.copyProperties(dto, po);
        waterQualityTestDAO.update(po);
    }

    public PageVO<WaterQualityTestPO> pageList(WaterQualityTestQueryDTO dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        WaterQualityTestPO po = new WaterQualityTestPO();
        BeanUtils.copyProperties(dto, po);
        return PageVO.toPageVO(waterQualityTestDAO.listAll(po));
    }

    public WaterQualityTestPO getById(IdDTO<Long> dto) {
        return waterQualityTestDAO.getById(dto.getId());
    }
}